package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.bean.CommodityEvaluateBean;
import com.afkt.project.ui.adapter.GreenDaoAdapter;
import com.afkt.project.util.LogUtils;
import com.afkt.project.util.ToastUtil;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.helper.ViewHelper;
import com.dev.utils.common.RandomUtils;
import com.dev.widget.ui.sliding.SlidingLayout;
import com.dev.widget.ui.sliding.SwipeActionHelper;
import com.dev.widget.ui.sliding.SwipeDirection;
import com.ohos.afkt.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * detail: Room RoomActivity
 *
 * @author Ttt
 */
public class RoomActivity extends BaseActivity  implements SwipeActionHelper.ContactCallbacks {

    private final static String TAG = "RoomActivity";

    private ListContainer vidBvrList;

    private DirectionalLayout bar_layout;

    private DirectionalLayout bar;
    private SlidingLayout slidin;

    private Image select_retuen;

    private Text bar_name;
    private Button add;

    // 适配器
    GreenDaoAdapter multiSelectAdapter;


    private List<CommodityEvaluateBean> lists = new ArrayList<>();

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_base_view_listContainer_greendao;
    }

    @Override
    protected void initView() {
        LogUtils.error(TAG, "initView()");
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_select_bar_layout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_multiselectassist_bar, null, false);
        bar_name = (Text) bar.findComponentById(ResourceTable.Id_bar_name);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_select_return);

        bar_layout.addComponent(bar);
        bar_name.setText("Room");
        add = (Button) findComponentById(ResourceTable.Id_btn_add);
        vidBvrList = (ListContainer) findComponentById(ResourceTable.Id_select_bvr_list);
        slidin=(SlidingLayout)findComponentById(ResourceTable.Id_slidin);
        ComponentContainer parent = vidBvrList;
        // 根布局处理
        ViewHelper.get().setPadding(parent, 0)
                .setBackgroundColor(parent, ResourceUtils.getColorForInt(ResourceTable.Color_color_33));
        for (int i = 0; i < 15; i++) {
            lists.add(CommodityEvaluateBean.newCommodityEvaluateBean());
        }

        // 初始化布局管理器、适配器
        multiSelectAdapter = new GreenDaoAdapter(vidBvrList,lists);
        vidBvrList.setItemProvider(multiSelectAdapter);
        add.setClickedListener(component -> {
            int mumber = RandomUtils.getRandom(1, 10);
            for (int i = 0; i < mumber; i++) {
                lists.add(CommodityEvaluateBean.newCommodityEvaluateBean());
            }
            multiSelectAdapter.notifyDataChanged();
            ToastUtil.showToast(RoomActivity.this,"添加"+mumber+"条数据成功，上拉加载数据");
        });
    }


    @Override
    protected void initListener() {
        select_retuen.setClickedListener(component -> {
            LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(bar);
        super.onBackground();
    }


    @Override
    public boolean hasActions(int position, SwipeDirection direction) {
        return false;
    }

    @Override
    public boolean shouldDismiss(int position, SwipeDirection direction) {
        return false;
    }

    @Override
    public void onSwipe(int position, SwipeDirection direction) {
        String dir = "";
        switch (direction) {
            case DIRECTION_FAR_LEFT:
                dir = "Far left";
                break;
            case DIRECTION_NORMAL_LEFT:
                dir = "Left";
                break;
            case DIRECTION_FAR_RIGHT:
                dir = "Far right";
                break;
            case DIRECTION_NORMAL_RIGHT:
                dir = "Right";
                break;
        }
        ToastDialog builder = new ToastDialog(this);
        builder.setText(dir ).show();
    }
}
